package com.jamie.jvm.singleton;

public class SingletonDemo {

    private static volatile SingletonDemo instance = null;

    private SingletonDemo() {
        System.out.println(Thread.currentThread().getName() + " 我是构造方法SingletonDemo()");
    }

    /**
     * DCL模式 Double Check Lock 双端检测锁机制
     */
    private static SingletonDemo getInstance() {
        if(instance == null){
            synchronized(SingletonDemo.class){
                if(instance == null){
                    instance = new SingletonDemo();
                }
            }
        }
        return instance;
    }

    public static void main(String[] args) {
        // 单线程
        //        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
        //        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());
        //        System.out.println(SingletonDemo.getInstance() == SingletonDemo.getInstance());

        // 多线程
        for(int i = 0; i < 10; i++){
            new Thread(SingletonDemo::getInstance, String.valueOf(i)).start();
        }
    }

}
